/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#include "osloader.h"

#include <bootoutput.h>
#include <machine/machine.h>
#include <machine/pc/memory/memmanager.h>
#include <kerneldebugconsole.h>
#include <bootstrapinfo.h>

extern void *end;

PLOS_Loader::PLOS_Loader() {
	m_endKernel = reinterpret_cast<uintptr_t>(&end);
}

void PLOS_Loader::start(unsigned long address) {
	PLBootOutput::instance().printf("Loading BubblesOS ");
	PLKernelDebugConsole::instance().cls();
	PLKernelDebugConsole::instance().printf("Kernel debug messages:\n");
	PLMachine &machine = PLMachine::instance();
	machine.setMultiboot(address);
	Multiboot_info *minfo = machine.multibootInfo();
	uint32_t i;
	Multiboot_moduleList *modEntry;
	for (i = 0, modEntry = (Multiboot_moduleList *)minfo->mods_addr ; i < minfo->mods_count ; i++, modEntry++) {
		PLKernelDebugConsole::instance().printf("Module: 0x%x 0x%x 0x%x %s\n", modEntry->mod_start, modEntry->mod_end, modEntry->mod_cmdline, modEntry->mod_cmdline);
		if(modEntry->mod_end > m_endModules) {
			m_endModules = modEntry->mod_end + 1;
		}
	}
	PLKernelDebugConsole::instance().printf("End of kernel data was: 0x%x", m_endModules);
	PLMachine::instance().kernelMemManger()->m_staticHandler.init();
	machine.readMultibootInformtion();
	machine.start();
}
